<?php

$page = "index";
if (array_key_exists("page", $_POST)) $page = $_POST["page"];
if (array_key_exists("page", $_GET))  $page = $_GET["page"];

include_once("root.php");
include_once($ROOT . "base.inc.php");

// Security Scheme.  Configure the roles and what pages
//   they have access to.
$user = UserUtil::getCurrentUser();

if ($user == false)
{
	$page = "login";
	$userRoles = array("");
}
else
{
	$userId = UserUtil::getCurrentUser()->id;
	$userRoles = UserUtil::getRoles($userId);
	if (!UserUtil::hasPermission($userRoles, $page) && $page != "logout")
	{
		// If the user doesn't have permission, redirect them to another
		//   location.
		$page = "noPermission";
	}
}
include_once($ROOT . "control/$page" . "Controller.php");
$PHP_SELF = $_SERVER['PHP_SELF'] . "?page=" . $page;
$c = & new PageController();
?>

<html>
	<head>
		<title><?php echo $appTitle . $c->title; ?></title>
		<link rel="stylesheet" type="text/css" href="css/base.css" />
		<script language="Javascript" type="text/javascript" src="js/core.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/coordinates.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/css.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/events.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/drag.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/dragsort.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/cookies.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/ajax.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/datepicker.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/misc.js" ></script>
		<script language="Javascript" type="text/javascript" src="js/bsn.Ajax.js"></script>
		<script language="Javascript" type="text/javascript" src="js/bsn.DOM.js"></script>
		<script language="Javascript" type="text/javascript" src="js/bsn.AutoSuggest.js"></script>
	</head>

	<body>
		<table border=0 width="100%">
			<tr>
				<td style="height:50" class="title">
					<table width="100%" border=0><tr><td><h1><?php echo $appTitle; ?></h1></td>
						<?php if ($userId != 0 && $userId != "") { ?>
						<td align="right" valign="top"><small><a href="?page=logout">[logout]</a></small></td></tr>
						<?php } ?>
					</table>
					<div id="menu">
					  <ul>
						<?php
						foreach ($__NAV as $section => $nav)
						{
							if (UserUtil::hasPermission($userRoles, $nav->page))
							{
								echo  "\t\t\t\t\t<li";
								if ($c->section == $section)  echo " id=\"current\"";
								echo "><a href=\"?page=$nav->page\">$nav->label</a></li>\n";
							}
							$i++;
						}
						?>
					  </ul>
					</div>
				</td>
			</tr>
			<?php if (array_key_exists($c->section, $__NAV) &&  count($__NAV[$c->section]->subNav ) > 0) { ?>
			<tr>
				<td>
					<div id="submenu">
						<ul>
							<li>|</li>
						<?php
						foreach ($__NAV[$c->section]->subNav as $nav)
						{
							if (UserUtil::hasPermission($userRoles, $nav->page))
							{
								echo  "\t\t\t\t\t<li><a href=\"?page=$nav->page\">$nav->label</a> &nbsp; | </li>\n";
							}
						}
						?>
						</ul>
					</div>
				</td>
			</tr>
			<?php } ?>
			<tr>
				<td>
				  <?php echo displayErrors($c->errs); ?>
				  <?php echo displayMessages($c->messages); ?>
				</td>
			</tr>
			<tr>
				<td>
	  				<?php include_once("pages/$page.php"); ?>
	  			</td>
	  		</tr>
	  	</table>
	</body>
</html>